<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Http;
use think\Session;

class Index extends Frontend
{
    // 微信文档
    // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $gettemplate_url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate';
    protected $send_url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
    protected $getpubtemplatekeywords_url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords';
    
    public function index()
    {
        return $this->view->fetch();
    }

    // 获取当前帐号下的个人模板列表
    public function getTemplateList()
    {
        $token['access_token'] = $this->getAccessToken();
        $response = Http::get($this->gettemplate_url, $token);
        $ret = (array)json_decode($response, true);
        var_dump($ret);die;

        return $this->view->fetch();
    }

    // 获取当前帐号下的个人模板列表
    public function getpubtemplatekeywords_url()
    {

        $tid = 'o4P_q_Q6Z8RBCpBUKbyaIJhegTSS1nqo6UPj4S--_oY';
        $token = $this->getAccessToken();
        $queryarr = array(
            "access_token"      => $token,
            "tid"     => $tid, // 接收者（用户）的 openid
        );
        $response = Http::get($this->getpubtemplatekeywords_url, $queryarr);
        $ret = (array)json_decode($response, true);
        var_dump($ret);die;

        return $this->view->fetch();
    }



    // 发送订阅消息
    public function send()
    {
        $token = $this->getAccessToken();

        $queryarr = array(
            "access_token"      => $token,
            // "touser"     => $config['AppSecret'], // 接收者（用户）的 openid
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id
            // "template_id" => $config['AppSecret'], // 所需下发的订阅模板id

        );

        $response = Http::get($this->send_url, $token);
        $ret = (array)json_decode($response, true);
        var_dump($ret);die;
    }

    /**
     * 获取access_token
     * @param string code
     * @return array
     */
    public function getAccessToken()
    {

        $access_token = cache('access_token');
        if ($access_token) {
            return $access_token;
        }

        $config = get_addon_config('litestore');
        $queryarr = array(
            "appid"      => $config['AppID'],
            "secret"     => $config['AppSecret'],
            "grant_type" => "client_credential",
        );
        $response = Http::get('https://api.weixin.qq.com/cgi-bin/token', $queryarr);
        $ret = (array)json_decode($response, true);
        if ($ret) {
            cache('access_token', $ret['access_token'], $ret['expires_in']);
        }
        return $ret ? $ret['access_token'] : [];
    }
}
